<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>WeakMap</title>
</head>
<body>
  <script>
    const key1 = {id: 1},
          key2 = {id: 2},
          key3 = {id: 3};
    const wm1 = new WeakMap([
      [key1, "val1"],
      [key2, "val2"],
      [key3, "val3"]
    ]);
    console.log(wm1.get(key1));  // val1
    console.log(wm1.get(key2));  // val2
    console.log(wm1.get(key3));  // val3
  </script>

  <!-- <script>
    /*
    初始化是全有或全无的操作
    只要有一个键无效就会抛出错误，导致整个初始化失败
     */
    const wm2 = new WeakMap([
      [key1, "val1"],
      ["BADKEY", "val2"],
      [key3, "val3"]
    ]);  // TypeError
  </script> -->
  <script>
    // 原始值可以先包装成对象再用作键
    const stringKey = new String("key1");
    const wm3 = new WeakMap([
      [stringKey, "val1"]
    ]);
    console.log(wm3.get(stringKey));  // "val1"
  </script>
</body>
</html>